Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lower integration tests memory usage #327

Merged
merged 8 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cmd/sonicd/app/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func initFlags() {
}
performanceFlags = []cli.Flag{
flags.CacheFlag,
flags.LiveDbCacheFlag,
flags.ArchiveCacheFlag,
}
networkingFlags = []cli.Flag{
flags.BootnodesFlag,
Expand Down Expand Up @@ -222,6 +224,15 @@ func lachesisMain(ctx *cli.Context) error {
}

metrics.SetDataDir(cfg.Node.DataDir) // report disk space usage into metrics
liveCache := ctx.Int64(flags.LiveDbCacheFlag.Name)
if liveCache > 0 {
cfg.OperaStore.EVM.StateDb.LiveCache = liveCache
}

archiveCache := ctx.Int64(flags.ArchiveCacheFlag.Name)
if archiveCache > 0 {
cfg.OperaStore.EVM.StateDb.ArchiveCache = archiveCache
}

node, _, nodeClose, err := config.MakeNode(ctx, cfg)
if err != nil {
Expand Down
12 changes: 9 additions & 3 deletions cmd/sonicd/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"fmt"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
"os"
"strings"
"testing"
Expand All @@ -11,8 +12,6 @@ import (
"github.com/Fantom-foundation/go-opera/integration/makefakegenesis"
futils "github.com/Fantom-foundation/go-opera/utils"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"

"github.com/docker/docker/pkg/reexec"
"github.com/ethereum/go-ethereum/common"

Expand All @@ -26,7 +25,14 @@ func tmpdir(t *testing.T) string {
func initFakenetDatadir(dataDir string, validatorsNum idx.Validator) {
genesisStore := makefakegenesis.FakeGenesisStore(validatorsNum, futils.ToFtm(1000000000), futils.ToFtm(5000000))
defer genesisStore.Close()
if err := genesis.ImportGenesisStore(genesisStore, dataDir, false, cachescale.Identity); err != nil {

if err := genesis.ImportGenesisStore(genesis.ImportParams{
GenesisStore: genesisStore,
DataDir: dataDir,
CacheRatio: cachescale.Identity,
LiveDbCache: 1, // Set lowest cache
ArchiveCache: 1, // Set lowest cache
}); err != nil {
panic(err)
}
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/sonictool/app/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"compress/gzip"
"fmt"
"github.com/Fantom-foundation/go-opera/cmd/sonictool/db"
"io"
"os"
"strconv"
Expand Down Expand Up @@ -57,8 +58,14 @@ func exportEvents(ctx *cli.Context) error {
to = idx.Epoch(n)
}

gdbParams := db.GossipDbParameters{
DataDir: dataDir,
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
}

log.Info("Exporting events to file", "file", fn)
err = chain.ExportEvents(writer, dataDir, from, to)
err = chain.ExportEvents(gdbParams, writer, dataDir, from, to)
cabrador marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("export error: %w", err)
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/sonictool/app/export_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/Fantom-foundation/go-opera/cmd/sonictool/genesis"
"github.com/Fantom-foundation/go-opera/config/flags"
"github.com/Fantom-foundation/go-opera/integration"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
"github.com/syndtr/goleveldb/leveldb/opt"
"gopkg.in/urfave/cli.v1"
"os"
Expand Down Expand Up @@ -48,7 +47,14 @@ func exportGenesis(ctx *cli.Context) error {
}
defer dbs.Close()

gdb, err := db.MakeGossipDb(dbs, dataDir, false, cachescale.Identity)
gdb, err := db.MakeGossipDb(db.GossipDbParameters{
Dbs: dbs,
DataDir: dataDir,
ValidatorMode: false,
CacheRatio: cacheRatio,
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
})
if err != nil {
return err
}
Expand Down
27 changes: 24 additions & 3 deletions cmd/sonictool/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ func gfileGenesisImport(ctx *cli.Context) error {
return fmt.Errorf("genesis file check failed: %w", err)
}
}
return genesis.ImportGenesisStore(genesisStore, dataDir, validatorMode, cacheRatio)
return genesis.ImportGenesisStore(genesis.ImportParams{
GenesisStore: genesisStore,
DataDir: dataDir,
ValidatorMode: validatorMode,
CacheRatio: cacheRatio,
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
})
}

func jsonGenesisImport(ctx *cli.Context) error {
Expand Down Expand Up @@ -98,7 +105,14 @@ func jsonGenesisImport(ctx *cli.Context) error {
return fmt.Errorf("failed to prepare JSON genesis: %w", err)
}
defer genesisStore.Close()
return genesis.ImportGenesisStore(genesisStore, dataDir, validatorMode, cacheRatio)
return genesis.ImportGenesisStore(genesis.ImportParams{
GenesisStore: genesisStore,
DataDir: dataDir,
ValidatorMode: validatorMode,
CacheRatio: cacheRatio,
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
})
}

func fakeGenesisImport(ctx *cli.Context) error {
Expand Down Expand Up @@ -127,7 +141,14 @@ func fakeGenesisImport(ctx *cli.Context) error {

genesisStore := makefakegenesis.FakeGenesisStore(idx.Validator(validatorsNumber), futils.ToFtm(1000000000), futils.ToFtm(5000000))
defer genesisStore.Close()
return genesis.ImportGenesisStore(genesisStore, dataDir, validatorMode, cacheRatio)
return genesis.ImportGenesisStore(genesis.ImportParams{
GenesisStore: genesisStore,
DataDir: dataDir,
ValidatorMode: validatorMode,
CacheRatio: cacheRatio,
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
})
}

func isValidatorModeSet(ctx *cli.Context) (bool, error) {
Expand Down
2 changes: 2 additions & 0 deletions cmd/sonictool/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func Run() error {
app.Flags = []cli.Flag{
flags.DataDirFlag,
flags.CacheFlag,
flags.LiveDbCacheFlag,
flags.ArchiveCacheFlag,
}
app.Commands = []cli.Command{
{
Expand Down
8 changes: 6 additions & 2 deletions cmd/sonictool/chain/export_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ var (
// always print out progress. This avoids the user wondering what's going on.
const statsReportLimit = 8 * time.Second

func ExportEvents(w io.Writer, dataDir string, from, to idx.Epoch) (err error) {
func ExportEvents(gdbParams db.GossipDbParameters, w io.Writer, dataDir string, from, to idx.Epoch) (err error) {
chaindataDir := filepath.Join(dataDir, "chaindata")
dbs, err := db.MakeDbProducer(chaindataDir, cachescale.Identity)
if err != nil {
return err
}
defer dbs.Close()

gdb, err := db.MakeGossipDb(dbs, dataDir, false, cachescale.Identity)
// Fill the rest of the params
gdbParams.Dbs = dbs
gdbParams.CacheRatio = cachescale.Identity

gdb, err := db.MakeGossipDb(gdbParams)
if err != nil {
return err
}
Expand Down
28 changes: 22 additions & 6 deletions cmd/sonictool/db/dbutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func makeDatabaseHandles() uint64 {
if err != nil {
panic(fmt.Errorf("failed to raise file descriptor allowance: %v", err))
}
return raised / 6 + 1
return raised/6 + 1
LuisPH3 marked this conversation as resolved.
Show resolved Hide resolved
}

func AssertDatabaseNotInitialized(dataDir string) error {
Expand Down Expand Up @@ -61,16 +61,32 @@ func MakeDbProducer(chaindataDir string, cacheRatio cachescale.Func) (kvdb.FullD
})
}

func MakeGossipDb(dbs kvdb.FullDBProducer, dataDir string, validatorMode bool, cacheRatio cachescale.Func) (*gossip.Store, error) {
gdbConfig := gossip.DefaultStoreConfig(cacheRatio)
gdbConfig.EVM.StateDb.Directory = filepath.Join(dataDir, "carmen")
if validatorMode {
// GossipDbParameters are parameters for GossipDb factory func.
type GossipDbParameters struct {
Dbs kvdb.FullDBProducer
DataDir string
ValidatorMode bool
CacheRatio cachescale.Func
LiveDbCache, ArchiveCache int64 // in bytes
}

func MakeGossipDb(params GossipDbParameters) (*gossip.Store, error) {
gdbConfig := gossip.DefaultStoreConfig(params.CacheRatio)
if params.ValidatorMode {
gdbConfig.EVM.StateDb.Archive = carmen.NoArchive
gdbConfig.EVM.DisableLogsIndexing = true
gdbConfig.EVM.DisableTxHashesIndexing = true
}

gdb, err := gossip.NewStore(dbs, gdbConfig)
if params.LiveDbCache > 0 {
gdbConfig.EVM.StateDb.LiveCache = params.LiveDbCache
}
if params.ArchiveCache > 0 {
gdbConfig.EVM.StateDb.ArchiveCache = params.ArchiveCache
}
gdbConfig.EVM.StateDb.Directory = filepath.Join(params.DataDir, "carmen")

gdb, err := gossip.NewStore(params.Dbs, gdbConfig)
if err != nil {
return nil, fmt.Errorf("failed to create gossip store: %w", err)
}
Expand Down
32 changes: 24 additions & 8 deletions cmd/sonictool/genesis/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,45 @@ import (
"path/filepath"
)

func ImportGenesisStore(genesisStore *genesisstore.Store, dataDir string, validatorMode bool, cacheRatio cachescale.Func) error {
if err := db.AssertDatabaseNotInitialized(dataDir); err != nil {
// ImportParams are parameters for ImportGenesisStore func.
type ImportParams struct {
GenesisStore *genesisstore.Store
DataDir string
ValidatorMode bool
CacheRatio cachescale.Func
LiveDbCache, ArchiveCache int64 // in bytes
}

func ImportGenesisStore(params ImportParams) error {
if err := db.AssertDatabaseNotInitialized(params.DataDir); err != nil {
return fmt.Errorf("database in datadir is already initialized: %w", err)
}
if err := db.RemoveDatabase(dataDir); err != nil {
if err := db.RemoveDatabase(params.DataDir); err != nil {
return fmt.Errorf("failed to remove existing data from the datadir: %w", err)
}

chaindataDir := filepath.Join(dataDir, "chaindata")
dbs, err := db.MakeDbProducer(chaindataDir, cacheRatio)
chaindataDir := filepath.Join(params.DataDir, "chaindata")
dbs, err := db.MakeDbProducer(chaindataDir, params.CacheRatio)
if err != nil {
return err
}
defer dbs.Close()
setGenesisProcessing(chaindataDir)

gdb, err := db.MakeGossipDb(dbs, dataDir, validatorMode, cacheRatio)
gdb, err := db.MakeGossipDb(db.GossipDbParameters{
Dbs: dbs,
DataDir: params.DataDir,
ValidatorMode: params.ValidatorMode,
CacheRatio: params.CacheRatio,
LiveDbCache: params.LiveDbCache,
ArchiveCache: params.ArchiveCache,
})
if err != nil {
return err
}
defer gdb.Close()

err = gdb.ApplyGenesis(genesisStore.Genesis())
err = gdb.ApplyGenesis(params.GenesisStore.Genesis())
if err != nil {
return fmt.Errorf("failed to write Gossip genesis state: %v", err)
}
Expand All @@ -54,7 +70,7 @@ func ImportGenesisStore(genesisStore *genesisstore.Store, dataDir string, valida
abftCrit := func(err error) {
panic(fmt.Errorf("lachesis store error: %w", err))
}
cdb := abft.NewStore(cMainDb, cGetEpochDB, abftCrit, abft.DefaultStoreConfig(cacheRatio))
cdb := abft.NewStore(cMainDb, cGetEpochDB, abftCrit, abft.DefaultStoreConfig(params.CacheRatio))
defer cdb.Close()

err = cdb.ApplyGenesis(&abft.Genesis{
Expand Down
17 changes: 17 additions & 0 deletions config/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package flags

import (
"fmt"
"strings"

"github.com/Fantom-foundation/go-opera/evmcore"
Expand Down Expand Up @@ -348,4 +349,20 @@ var (
Name: "lachesis.suppress-frame-panic",
Usage: "Suppress frame missmatch error (when testing on historical imported/synced events)",
}

// StateDb
LiveDbCacheFlag = cli.Int64Flag{
Name: "statedb.livecache",
Usage: fmt.Sprintf("Size of live db cache in bytes. Leaving this blank (which is generally recommended),"+
"or setting this to <1 will automatically allocate cache size depending on how much cache you use with %s."+
"Setting this value to <=2000 will result in pre-confugired cache capacity of 2KB", CacheFlag.Name),
cabrador marked this conversation as resolved.
Show resolved Hide resolved
Value: 0,
}
ArchiveCacheFlag = cli.IntFlag{
Name: "statedb.archivecache",
Usage: fmt.Sprintf("Size of archive cache in bytes. Leaving this blank (which is generally recommended),"+
"or setting this to <1 will automatically allocate cache size depending on how much cache you use with %s."+
"Setting this value to <=2000 will result in pre-confugired cache capacity of 2KB", CacheFlag.Name),
Value: 0,
}
)
20 changes: 17 additions & 3 deletions tests/integration_test_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,20 @@ func startIntegrationTestNet(directory string, args []string) (*IntegrationTestN
// initialize the data directory for the single node on the test network
// equivalent to running `sonictool --datadir <dataDir> genesis fake 1`
originalArgs := os.Args
os.Args = append([]string{"sonictool", "--datadir", result.stateDir()}, args...)
os.Args = append([]string{
"sonictool",
"--datadir", result.stateDir(),
"--statedb.livecache", "1",
LuisPH3 marked this conversation as resolved.
Show resolved Hide resolved
"--statedb.archivecache", "1",
LuisPH3 marked this conversation as resolved.
Show resolved Hide resolved
}, args...)
if err := sonictool.Run(); err != nil {
os.Args = originalArgs
return nil, fmt.Errorf("failed to initialize the test network: %w", err)
}
os.Args = originalArgs

os.Args = originalArgs
cabrador marked this conversation as resolved.
Show resolved Hide resolved

if err := result.start(); err != nil {
return nil, fmt.Errorf("failed to start the test network: %w", err)
}
Expand All @@ -200,10 +207,17 @@ func (n *IntegrationTestNet) start() error {
"sonicd",
"--datadir", n.stateDir(),
"--fakenet", "1/1",
"--http", "--http.addr", "0.0.0.0", "--http.port", "18545",
"--http",
"--http.addr", "0.0.0.0",
"--http.port", "18545",
"--http.api", "admin,eth,web3,net,txpool,ftm,trace,debug",
"--ws", "--ws.addr", "0.0.0.0", "--ws.port", "18546", "--ws.api", "admin,eth,ftm",
"--ws",
"--ws.addr", "0.0.0.0",
"--ws.port", "18546",
"--ws.api", "admin,eth,ftm",
"--datadir.minfreedisk", "0",
"--statedb.livecache", "1",
"--statedb.archivecache", "1",
}
sonicd.Run()
}()
Expand Down