2020-07-14 01:56:29 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2022-02-12 13:33:09 +00:00
|
|
|
"path/filepath"
|
2022-09-18 10:41:01 +00:00
|
|
|
"runtime"
|
2021-04-19 07:25:26 +00:00
|
|
|
|
2022-05-25 09:22:02 +00:00
|
|
|
"github.com/c2h5oh/datasize"
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
|
|
|
kv2 "github.com/ledgerwatch/erigon-lib/kv/mdbx"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
|
|
|
"github.com/ledgerwatch/erigon/internal/debug"
|
2022-10-21 05:36:17 +00:00
|
|
|
"github.com/ledgerwatch/erigon/internal/logging"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/migrations"
|
2021-07-29 10:23:23 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2020-07-14 01:56:29 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-07-15 06:17:07 +00:00
|
|
|
"github.com/torquem-ch/mdbx-go/mdbx"
|
2022-09-18 10:41:01 +00:00
|
|
|
"golang.org/x/sync/semaphore"
|
2020-07-14 01:56:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "integration",
|
2021-05-26 10:35:39 +00:00
|
|
|
Short: "long and heavy integration tests for Erigon",
|
2020-07-14 01:56:29 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
2022-10-21 05:36:17 +00:00
|
|
|
if err := debug.SetupCobra(cmd); err != nil {
|
2020-08-20 03:52:27 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-04-19 07:25:26 +00:00
|
|
|
if chaindata == "" {
|
2022-06-07 04:00:37 +00:00
|
|
|
chaindata = filepath.Join(datadirCli, "chaindata")
|
2021-04-19 07:25:26 +00:00
|
|
|
}
|
2020-07-14 01:56:29 +00:00
|
|
|
},
|
2020-08-20 03:52:27 +00:00
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
2022-10-21 05:36:17 +00:00
|
|
|
defer debug.Exit()
|
2020-08-20 03:52:27 +00:00
|
|
|
},
|
2020-07-14 01:56:29 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 11:46:20 +00:00
|
|
|
func RootCommand() *cobra.Command {
|
2022-10-21 05:36:17 +00:00
|
|
|
utils.CobraFlags(rootCmd, debug.Flags, utils.MetricFlags, logging.Flags)
|
2020-08-19 11:46:20 +00:00
|
|
|
return rootCmd
|
2020-07-14 01:56:29 +00:00
|
|
|
}
|
2020-10-13 22:42:04 +00:00
|
|
|
|
2022-08-12 14:45:09 +00:00
|
|
|
func dbCfg(label kv.Label, path string) kv2.MdbxOpts {
|
2022-09-18 10:41:01 +00:00
|
|
|
limiterB := semaphore.NewWeighted(int64(runtime.NumCPU()*10 + 1))
|
|
|
|
opts := kv2.NewMDBX(log.New()).Path(path).Label(label).RoTxsLimiter(limiterB)
|
2022-06-09 02:45:30 +00:00
|
|
|
if label == kv.ChainDB {
|
|
|
|
opts = opts.MapSize(8 * datasize.TB)
|
|
|
|
}
|
|
|
|
if databaseVerbosity != -1 {
|
|
|
|
opts = opts.DBVerbosity(kv.DBVerbosityLvl(databaseVerbosity))
|
|
|
|
}
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func openDB(opts kv2.MdbxOpts, applyMigrations bool) kv.RwDB {
|
2022-07-15 06:17:07 +00:00
|
|
|
// 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 })
|
2022-06-09 02:45:30 +00:00
|
|
|
db := opts.MustOpen()
|
2020-10-28 09:52:15 +00:00
|
|
|
if applyMigrations {
|
2022-06-09 02:45:30 +00:00
|
|
|
migrator := migrations.NewMigrator(opts.GetLabel())
|
|
|
|
has, err := migrator.HasPendingMigrations(db)
|
2021-03-01 04:02:22 +00:00
|
|
|
if err != nil {
|
2020-10-28 09:52:15 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-01 04:02:22 +00:00
|
|
|
if has {
|
|
|
|
log.Info("Re-Opening DB in exclusive mode to apply DB migrations")
|
|
|
|
db.Close()
|
2022-06-09 02:45:30 +00:00
|
|
|
db = opts.Exclusive().MustOpen()
|
|
|
|
if err := migrator.Apply(db, datadirCli); err != nil {
|
2021-03-01 04:02:22 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
db.Close()
|
2022-06-09 02:45:30 +00:00
|
|
|
db = opts.MustOpen()
|
2021-03-01 04:02:22 +00:00
|
|
|
}
|
2020-10-28 09:52:15 +00:00
|
|
|
}
|
2021-07-04 07:50:32 +00:00
|
|
|
return db
|
2020-10-28 09:52:15 +00:00
|
|
|
}
|