[devnet tool] Side-quest logging, step 5 (#7484)

Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro-2.local>
This commit is contained in:
ledgerwatch 2023-05-10 19:36:27 +01:00 committed by GitHub
parent 20437faaf5
commit 6ef3fc3a3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 64 additions and 53 deletions

View File

@ -171,7 +171,7 @@ func NewBackend(stack *node.Node, config *ethconfig.Config, logger log.Logger) (
}
// Assemble the Ethereum object
chainKv, err := node.OpenDatabase(stack.Config(), kv.ChainDB)
chainKv, err := node.OpenDatabase(stack.Config(), kv.ChainDB, logger)
if err != nil {
return nil, err
}

View File

@ -34,7 +34,7 @@ var cmdResetState = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return

View File

@ -54,7 +54,7 @@ func dbCfg(label kv.Label, path string) kv2.MdbxOpts {
return opts
}
func openDB(opts kv2.MdbxOpts, applyMigrations bool) (kv.RwDB, error) {
func openDB(opts kv2.MdbxOpts, applyMigrations bool, logger log.Logger) (kv.RwDB, error) {
// integration tool don't intent to create db, then easiest way to open db - it's pass mdbx.Accede flag, which allow
// to read all options from DB, instead of overriding them
opts = opts.Flags(func(f uint) uint { return f | mdbx.Accede })
@ -67,10 +67,10 @@ func openDB(opts kv2.MdbxOpts, applyMigrations bool) (kv.RwDB, error) {
return nil, err
}
if has {
log.Info("Re-Opening DB in exclusive mode to apply DB migrations")
logger.Info("Re-Opening DB in exclusive mode to apply DB migrations")
db.Close()
db = opts.Exclusive().MustOpen()
if err := migrator.Apply(db, datadirCli); err != nil {
if err := migrator.Apply(db, datadirCli, logger); err != nil {
return nil, err
}
db.Close()

View File

@ -63,7 +63,7 @@ var cmdStageSnapshots = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -89,7 +89,7 @@ var cmdStageHeaders = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -115,7 +115,7 @@ var cmdStageBodies = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -141,7 +141,7 @@ var cmdStageSenders = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -167,7 +167,7 @@ var cmdStageExec = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -195,7 +195,7 @@ var cmdStageTrie = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -221,7 +221,7 @@ var cmdStageHashState = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -247,7 +247,7 @@ var cmdStageHistory = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -273,7 +273,7 @@ var cmdLogIndex = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -299,7 +299,7 @@ var cmdCallTraces = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -325,7 +325,7 @@ var cmdStageTxLookup = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -350,7 +350,7 @@ var cmdPrintStages = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata).Readonly(), false)
db, err := openDB(dbCfg(kv.ChainDB, chaindata).Readonly(), false, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -376,7 +376,7 @@ var cmdPrintMigrations = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), false)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), false, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -401,7 +401,7 @@ var cmdRemoveMigration = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), false)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), false, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -426,7 +426,7 @@ var cmdRunMigrations = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -446,7 +446,7 @@ var cmdSetPrune = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -471,7 +471,7 @@ var cmdSetSnap = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -502,7 +502,7 @@ var cmdForceSetHistoryV3 = &cobra.Command{
logger.Error("Setting up", "error", err)
return
}
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return

View File

@ -127,7 +127,7 @@ var readDomains = &cobra.Command{
}
dirs := datadir.New(datadirCli)
chainDb, err := openDB(dbCfg(kv.ChainDB, dirs.Chaindata), true)
chainDb, err := openDB(dbCfg(kv.ChainDB, dirs.Chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -241,7 +241,7 @@ var stateDomains = &cobra.Command{
erigoncli.ApplyFlagsForEthConfigCobra(cmd.Flags(), ethConfig)
dirs := datadir.New(datadirCli)
chainDb, err := openDB(dbCfg(kv.ChainDB, dirs.Chaindata), true)
chainDb, err := openDB(dbCfg(kv.ChainDB, dirs.Chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return

View File

@ -69,7 +69,7 @@ Examples:
erigoncli.ApplyFlagsForEthConfigCobra(cmd.Flags(), ethConfig)
miningConfig := params.MiningConfig{}
utils.SetupMinerCobra(cmd, &miningConfig)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -104,7 +104,7 @@ var loopIhCmd = &cobra.Command{
return
}
ctx, _ := common2.RootContext()
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return
@ -133,7 +133,7 @@ var loopExecCmd = &cobra.Command{
return
}
ctx, _ := common2.RootContext()
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true)
db, err := openDB(dbCfg(kv.ChainDB, chaindata), true, logger)
if err != nil {
logger.Error("Opening DB", "error", err)
return

View File

@ -203,7 +203,7 @@ func New(stack *node.Node, config *ethconfig.Config, logger log.Logger) (*Ethere
}
// Assemble the Ethereum object
chainKv, err := node.OpenDatabase(stack.Config(), kv.ChainDB)
chainKv, err := node.OpenDatabase(stack.Config(), kv.ChainDB, logger)
if err != nil {
return nil, err
}

View File

@ -153,7 +153,7 @@ func (m *Migrator) VerifyVersion(db kv.RwDB) error {
return nil
}
func (m *Migrator) Apply(db kv.RwDB, dataDir string) error {
func (m *Migrator) Apply(db kv.RwDB, dataDir string, logger log.Logger) error {
if len(m.Migrations) == 0 {
return nil
}
@ -192,7 +192,7 @@ func (m *Migrator) Apply(db kv.RwDB, dataDir string) error {
callbackCalled := false // commit function must be called if no error, protection against people's mistake
log.Info("Apply migration", "name", v.Name)
logger.Info("Apply migration", "name", v.Name)
var progress []byte
if err := db.View(context.Background(), func(tx kv.Tx) (err error) {
progress, err = tx.GetOne(kv.Migrations, []byte("_progress_"+v.Name))
@ -235,7 +235,7 @@ func (m *Migrator) Apply(db kv.RwDB, dataDir string) error {
if !callbackCalled {
return fmt.Errorf("%w: %s", ErrMigrationCommitNotCalled, v.Name)
}
log.Info("Applied migration", "name", v.Name)
logger.Info("Applied migration", "name", v.Name)
}
// Write DB schema version
var version [12]byte
@ -250,7 +250,7 @@ func (m *Migrator) Apply(db kv.RwDB, dataDir string) error {
}); err != nil {
return fmt.Errorf("migrator.Apply: %w", err)
}
log.Info("Updated DB schema to", "version", fmt.Sprintf("%d.%d.%d", kv.DBSchemaVersion.Major, kv.DBSchemaVersion.Minor, kv.DBSchemaVersion.Patch))
logger.Info("Updated DB schema to", "version", fmt.Sprintf("%d.%d.%d", kv.DBSchemaVersion.Major, kv.DBSchemaVersion.Minor, kv.DBSchemaVersion.Patch))
return nil
}

View File

@ -9,6 +9,7 @@ import (
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/log/v3"
"github.com/stretchr/testify/require"
)
@ -49,7 +50,8 @@ func TestApplyWithInit(t *testing.T) {
migrator := NewMigrator(kv.ChainDB)
migrator.Migrations = m
err := migrator.Apply(db, "")
logger := log.New()
err := migrator.Apply(db, "", logger)
require.NoError(err)
var applied map[string][]byte
err = db.View(context.Background(), func(tx kv.Tx) error {
@ -65,7 +67,7 @@ func TestApplyWithInit(t *testing.T) {
require.NoError(err)
// apply again
err = migrator.Apply(db, "")
err = migrator.Apply(db, "", logger)
require.NoError(err)
err = db.View(context.Background(), func(tx kv.Tx) error {
applied2, err := AppliedMigrations(tx, false)
@ -109,7 +111,8 @@ func TestApplyWithoutInit(t *testing.T) {
migrator := NewMigrator(kv.ChainDB)
migrator.Migrations = m
err = migrator.Apply(db, "")
logger := log.New()
err = migrator.Apply(db, "", logger)
require.NoError(err)
var applied map[string][]byte
@ -127,7 +130,7 @@ func TestApplyWithoutInit(t *testing.T) {
require.NoError(err)
// apply again
err = migrator.Apply(db, "")
err = migrator.Apply(db, "", logger)
require.NoError(err)
err = db.View(context.Background(), func(tx kv.Tx) error {
@ -173,7 +176,8 @@ func TestWhenNonFirstMigrationAlreadyApplied(t *testing.T) {
migrator := NewMigrator(kv.ChainDB)
migrator.Migrations = m
err = migrator.Apply(db, "")
logger := log.New()
err = migrator.Apply(db, "", logger)
require.NoError(err)
var applied map[string][]byte
@ -191,7 +195,7 @@ func TestWhenNonFirstMigrationAlreadyApplied(t *testing.T) {
require.NoError(err)
// apply again
err = migrator.Apply(db, "")
err = migrator.Apply(db, "", logger)
require.NoError(err)
err = db.View(context.Background(), func(tx kv.Tx) error {
applied2, err := AppliedMigrations(tx, false)
@ -257,7 +261,8 @@ func TestValidation(t *testing.T) {
}
migrator := NewMigrator(kv.ChainDB)
migrator.Migrations = m
err := migrator.Apply(db, "")
logger := log.New()
err := migrator.Apply(db, "", logger)
require.True(errors.Is(err, ErrMigrationNonUniqueName))
var applied map[string][]byte
@ -283,7 +288,8 @@ func TestCommitCallRequired(t *testing.T) {
}
migrator := NewMigrator(kv.ChainDB)
migrator.Migrations = m
err := migrator.Apply(db, "")
logger := log.New()
err := migrator.Apply(db, "", logger)
require.True(errors.Is(err, ErrMigrationCommitNotCalled))
var applied map[string][]byte

View File

@ -12,6 +12,7 @@ import (
"github.com/ledgerwatch/erigon-lib/common/u256"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/log/v3"
"github.com/stretchr/testify/require"
"github.com/ledgerwatch/erigon/core/rawdb"
@ -60,7 +61,8 @@ func TestTxsBeginEnd(t *testing.T) {
migrator := NewMigrator(kv.ChainDB)
migrator.Migrations = []Migration{txsBeginEnd}
err = migrator.Apply(db, tmpDir)
logger := log.New()
err = migrator.Apply(db, tmpDir, logger)
require.NoError(err)
err = db.View(context.Background(), func(tx kv.Tx) error {

View File

@ -281,7 +281,7 @@ func (n *Node) DataDir() string {
return n.config.Dirs.DataDir
}
func OpenDatabase(config *nodecfg.Config, label kv.Label) (kv.RwDB, error) {
func OpenDatabase(config *nodecfg.Config, label kv.Label, logger log.Logger) (kv.RwDB, error) {
var name string
switch label {
case kv.ChainDB:
@ -299,7 +299,7 @@ func OpenDatabase(config *nodecfg.Config, label kv.Label) (kv.RwDB, error) {
dbPath := filepath.Join(config.Dirs.DataDir, name)
var openFunc func(exclusive bool) (kv.RwDB, error)
log.Info("Opening Database", "label", name, "path", dbPath)
logger.Info("Opening Database", "label", name, "path", dbPath)
openFunc = func(exclusive bool) (kv.RwDB, error) {
roTxLimit := int64(32)
if config.Http.DBReadConcurrency > 0 {
@ -339,13 +339,13 @@ func OpenDatabase(config *nodecfg.Config, label kv.Label) (kv.RwDB, error) {
return nil, err
}
if has {
log.Info("Re-Opening DB in exclusive mode to apply migrations")
logger.Info("Re-Opening DB in exclusive mode to apply migrations")
db.Close()
db, err = openFunc(true)
if err != nil {
return nil, err
}
if err = migrator.Apply(db, config.Dirs.DataDir); err != nil {
if err = migrator.Apply(db, config.Dirs.DataDir, logger); err != nil {
return nil, err
}
db.Close()

View File

@ -143,10 +143,11 @@ func TestNodeCloseClosesDB(t *testing.T) {
t.Skip("fix me on win please")
}
stack, _ := New(testNodeConfig(t), log.New())
logger := log.New()
stack, _ := New(testNodeConfig(t), logger)
defer stack.Close()
db, err := OpenDatabase(stack.Config(), kv.SentryDB)
db, err := OpenDatabase(stack.Config(), kv.SentryDB, logger)
if err != nil {
t.Fatal("can't open DB:", err)
}
@ -170,14 +171,15 @@ func TestNodeOpenDatabaseFromLifecycleStart(t *testing.T) {
t.Skip("fix me on win please")
}
stack, err := New(testNodeConfig(t), log.New())
logger := log.New()
stack, err := New(testNodeConfig(t), logger)
require.NoError(t, err)
defer stack.Close()
var db kv.RwDB
stack.RegisterLifecycle(&InstrumentedService{
startHook: func() {
db, err = OpenDatabase(stack.Config(), kv.SentryDB)
db, err = OpenDatabase(stack.Config(), kv.SentryDB, logger)
if err != nil {
t.Fatal("can't open DB:", err)
}
@ -197,12 +199,13 @@ func TestNodeOpenDatabaseFromLifecycleStop(t *testing.T) {
t.Skip("fix me on win please")
}
stack, _ := New(testNodeConfig(t), log.New())
logger := log.New()
stack, _ := New(testNodeConfig(t), logger)
defer stack.Close()
stack.RegisterLifecycle(&InstrumentedService{
stopHook: func() {
db, err := OpenDatabase(stack.Config(), kv.ChainDB)
db, err := OpenDatabase(stack.Config(), kv.ChainDB, logger)
if err != nil {
t.Fatal("can't open DB:", err)
}

View File

@ -61,7 +61,7 @@ func initGenesis(ctx *cli.Context) error {
stack := MakeConfigNodeDefault(ctx, logger)
defer stack.Close()
chaindb, err := node.OpenDatabase(stack.Config(), kv.ChainDB)
chaindb, err := node.OpenDatabase(stack.Config(), kv.ChainDB, logger)
if err != nil {
utils.Fatalf("Failed to open database: %v", err)
}