Skip to content

Commit

Permalink
feat: support custom db options
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 authored and pythonberg1997 committed Jul 3, 2023
1 parent 99243e4 commit bd545a7
Showing 1 changed file with 68 additions and 6 deletions.
74 changes: 68 additions & 6 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

db "github.com/cometbft/cometbft-db"
dbm "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/abci/server"
tcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
"github.com/cometbft/cometbft/node"
Expand Down Expand Up @@ -85,6 +86,16 @@ const (

// mempool flags
FlagMempoolMaxTxs = "mempool.max-txs"

// db-related flags
FlagDBCache = "db.cache"
FlagDBFDLimit = "db.fdlimit"
FlagDBFilter = "db.filter"
FlagDBApplicationPercentage = "db.application-percentage"
FlagDBBlockStorePercentage = "db.blockStore-percentage"
FlagDBStatePercentage = "db.state-percentage"
FlagDBTxIndexPercentage = "db.txIndex-percentage"
FlagDBEvidencePercentage = "db.evidence-percentage"
)

// StartCmd runs the service passed in, either stand-alone or in-process with
Expand Down Expand Up @@ -201,6 +212,15 @@ is performed. Note, when enabled, gRPC will also be automatically enabled.

cmd.Flags().Int(FlagMempoolMaxTxs, mempool.DefaultMaxTx, "Sets MaxTx value for the app-side mempool")

cmd.Flags().Int(FlagDBCache, 1024, "Megabytes of memory allocated to database caching")
cmd.Flags().Int(FlagDBFDLimit, 500, "Raise the open file descriptor resource limit")
cmd.Flags().Int(FlagDBFilter, 10, "The number of bits for the hot and cold Bloom filters in the database")
cmd.Flags().Int(FlagDBApplicationPercentage, 40, "Percentage of cache memory allowance to use for application database io")
cmd.Flags().Int(FlagDBBlockStorePercentage, 20, "Percentage of cache memory allowance to use for blockstore database io")
cmd.Flags().Int(FlagDBStatePercentage, 20, "Percentage of cache memory allowance to use for state database io")
cmd.Flags().Int(FlagDBTxIndexPercentage, 10, "Percentage of cache memory allowance to use for tx index database io")
cmd.Flags().Int(FlagDBEvidencePercentage, 10, "Percentage of cache memory allowance to use for evidence database io")

// add support for all Tendermint-specific command line options
tcmd.AddNodeFlags(cmd)
return cmd
Expand All @@ -211,9 +231,14 @@ func startStandAlone(ctx *Context, appCreator types.AppCreator) error {
transport := ctx.Viper.GetString(flagTransport)
home := ctx.Viper.GetString(flags.FlagHome)

dbCacheSize := ctx.Viper.GetInt(FlagDBCache) * ctx.Viper.GetInt(FlagDBApplicationPercentage) / 100 * opt.MiB
dbHandles := ctx.Viper.GetInt(FlagDBFDLimit) * ctx.Viper.GetInt(FlagDBApplicationPercentage) / 100
dbFilter := ctx.Viper.GetInt(FlagDBFilter)

db, err := openDB(home, GetAppDBBackend(ctx.Viper), &db.NewDatabaseOption{
Cache: 24 * opt.GiB,
Handles: 5120,
Cache: dbCacheSize,
Handles: dbHandles,
Filter: dbFilter,
})
if err != nil {
return err
Expand Down Expand Up @@ -275,9 +300,14 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
cfg := ctx.Config
home := cfg.RootDir

dbCacheSize := ctx.Viper.GetInt(FlagDBCache) * ctx.Viper.GetInt(FlagDBApplicationPercentage) / 100 * opt.MiB
dbHandles := ctx.Viper.GetInt(FlagDBFDLimit) * ctx.Viper.GetInt(FlagDBApplicationPercentage) / 100
dbFilter := ctx.Viper.GetInt(FlagDBFilter)

db, err := openDB(home, GetAppDBBackend(ctx.Viper), &db.NewDatabaseOption{
Cache: 24 * opt.GiB,
Handles: 5120,
Cache: dbCacheSize,
Handles: dbHandles,
Filter: dbFilter,
})
if err != nil {
return err
Expand Down Expand Up @@ -332,14 +362,14 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
config.GRPC.Enable = true
} else {
ctx.Logger.Info("starting node with ABCI Tendermint in-process")

dbOptions := makeDBOptions(ctx)
tmNode, err = node.NewNode(
cfg,
pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile()),
nodeKey,
proxy.NewLocalClientCreator(app),
genDocProvider,
node.DefaultDBProvider,
node.DefaultDBProviderWithDBOptions(dbOptions),
node.DefaultMetricsProvider(cfg.Instrumentation),
ctx.Logger,
)
Expand Down Expand Up @@ -582,3 +612,35 @@ func wrapCPUProfile(ctx *Context, callback func() error) error {

return WaitForQuitSignals()
}

// makeDBOptions returns the db options based on the context.
// The db options are used to create the db provider.
// such types of db are used in cometbft:
// - blockstore
// - state
// - tx_index
// - evidence
func makeDBOptions(ctx *Context) map[string]*dbm.NewDatabaseOption {
return map[string]*dbm.NewDatabaseOption{
"blockstore": {
Cache: ctx.Viper.GetInt(FlagDBCache) * ctx.Viper.GetInt(FlagDBBlockStorePercentage) / 100 * opt.MiB,
Handles: ctx.Viper.GetInt(FlagDBFDLimit) * ctx.Viper.GetInt(FlagDBBlockStorePercentage) / 100,
Filter: ctx.Viper.GetInt(FlagDBFilter),
},
"state": {
Cache: ctx.Viper.GetInt(FlagDBCache) * ctx.Viper.GetInt(FlagDBStatePercentage) / 100 * opt.MiB,
Handles: ctx.Viper.GetInt(FlagDBFDLimit) * ctx.Viper.GetInt(FlagDBStatePercentage) / 100,
Filter: ctx.Viper.GetInt(FlagDBFilter),
},
"tx_index": {
Cache: ctx.Viper.GetInt(FlagDBCache) * ctx.Viper.GetInt(FlagDBTxIndexPercentage) / 100 * opt.MiB,
Handles: ctx.Viper.GetInt(FlagDBFDLimit) * ctx.Viper.GetInt(FlagDBTxIndexPercentage) / 100,
Filter: ctx.Viper.GetInt(FlagDBFilter),
},
"evidence": {
Cache: ctx.Viper.GetInt(FlagDBCache) * ctx.Viper.GetInt(FlagDBEvidencePercentage) / 100 * opt.MiB,
Handles: ctx.Viper.GetInt(FlagDBFDLimit) * ctx.Viper.GetInt(FlagDBEvidencePercentage) / 100,
Filter: ctx.Viper.GetInt(FlagDBFilter),
},
}
}

0 comments on commit bd545a7

Please sign in to comment.