Skip to content

Commit

Permalink
Add --db-max-handles (#1587)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak authored and wojciechos committed Dec 20, 2023
1 parent eced4f5 commit 09a2cf7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cmd/juno/juno.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const (
remoteDBF = "remote-db"
rpcMaxBlockScanF = "rpc-max-block-scan"
dbCacheSizeF = "db-cache-size"
dbMaxHandlesF = "db-max-handles"
gwAPIKeyF = "gw-api-key" //nolint: gosec

defaultConfig = ""
Expand All @@ -86,6 +87,7 @@ const (
defaultRemoteDB = ""
defaultRPCMaxBlockScan = math.MaxUint
defaultCacheSizeMb = 8
defaultMaxHandles = 1024
defaultGwAPIKey = ""

configFlagUsage = "The yaml configuration file."
Expand Down Expand Up @@ -119,6 +121,7 @@ const (
remoteDBUsage = "gRPC URL of a remote Juno node"
rpcMaxBlockScanUsage = "Maximum number of blocks scanned in single starknet_getEvents call"
dbCacheSizeUsage = "Determines the amount of memory (in megabytes) allocated for caching data in the database."
dbMaxHandlesUsage = "A soft limit on the number of open files that can be used by the DB"
gwAPIKeyUsage = "API key for gateway endpoints to avoid throttling" //nolint: gosec
)

Expand Down Expand Up @@ -249,6 +252,7 @@ func NewCmd(config *node.Config, run func(*cobra.Command, []string) error) *cobr
junoCmd.Flags().Uint(rpcMaxBlockScanF, defaultRPCMaxBlockScan, rpcMaxBlockScanUsage)
junoCmd.Flags().Uint(dbCacheSizeF, defaultCacheSizeMb, dbCacheSizeUsage)
junoCmd.Flags().String(gwAPIKeyF, defaultGwAPIKey, gwAPIKeyUsage)
junoCmd.Flags().Int(dbMaxHandlesF, defaultMaxHandles, dbMaxHandlesUsage)

return junoCmd
}
14 changes: 14 additions & 0 deletions cmd/juno/juno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestConfigPrecedence(t *testing.T) {
defaultMaxVMs := uint(3 * runtime.GOMAXPROCS(0))
defaultRPCMaxBlockScan := uint(math.MaxUint)
defaultMaxCacheSize := uint(8)
defaultMaxHandles := 1024

tests := map[string]struct {
cfgFile bool
Expand Down Expand Up @@ -82,6 +83,7 @@ func TestConfigPrecedence(t *testing.T) {
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
},
},
"config file path is empty string": {
Expand Down Expand Up @@ -111,6 +113,7 @@ func TestConfigPrecedence(t *testing.T) {
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
},
},
"config file doesn't exist": {
Expand Down Expand Up @@ -145,6 +148,7 @@ func TestConfigPrecedence(t *testing.T) {
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
},
},
"config file with all settings but without any other flags": {
Expand Down Expand Up @@ -181,6 +185,7 @@ pprof: true
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
},
},
"config file with some settings but without any other flags": {
Expand Down Expand Up @@ -214,6 +219,7 @@ http-port: 4576
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
},
},
"all flags without config file": {
Expand Down Expand Up @@ -245,6 +251,7 @@ http-port: 4576
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
},
},
"some flags without config file": {
Expand Down Expand Up @@ -277,6 +284,7 @@ http-port: 4576
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
},
},
"all setting set in both config file and flags": {
Expand Down Expand Up @@ -333,6 +341,7 @@ db-cache-size: 8
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: 9,
DBMaxHandles: defaultMaxHandles,
},
},
"some setting set in both config file and flags": {
Expand Down Expand Up @@ -368,6 +377,7 @@ network: goerli
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
},
},
"some setting set in default, config file and flags": {
Expand Down Expand Up @@ -399,6 +409,7 @@ network: goerli
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
},
},
"only set env variables": {
Expand Down Expand Up @@ -428,6 +439,7 @@ network: goerli
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
},
},
"some setting set in both env variables and flags": {
Expand Down Expand Up @@ -458,6 +470,7 @@ network: goerli
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
DBMaxHandles: defaultMaxHandles,
},
},
"some setting set in both env variables and config file": {
Expand Down Expand Up @@ -489,6 +502,7 @@ network: goerli
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
GatewayAPIKey: "apikey",
DBMaxHandles: defaultMaxHandles,
},
},
}
Expand Down
7 changes: 4 additions & 3 deletions db/pebble/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ type DB struct {
}

// New opens a new database at the given path
func New(path string, cache uint, logger pebble.Logger) (db.DB, error) {
func New(path string, cache uint, maxOpenFiles int, logger pebble.Logger) (db.DB, error) {
// Ensure that the specified cache size meets a minimum threshold.
if cache < minCache {
cache = minCache
}
pDB, err := newPebble(path, &pebble.Options{
Logger: logger,
Cache: pebble.NewCache(int64(cache * megabyte)),
Logger: logger,
Cache: pebble.NewCache(int64(cache * megabyte)),
MaxOpenFiles: maxOpenFiles,
})
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ type Config struct {
MaxVMQueue uint `mapstructure:"max-vm-queue"`
RPCMaxBlockScan uint `mapstructure:"rpc-max-block-scan"`

DBCacheSize uint `mapstructure:"db-cache-size"`
DBCacheSize uint `mapstructure:"db-cache-size"`
DBMaxHandles int `mapstructure:"db-max-handles"`

GatewayAPIKey string `mapstructure:"gw-api-key"`
}
Expand Down Expand Up @@ -111,7 +112,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
if dbIsRemote {
database, err = remote.New(cfg.RemoteDB, context.TODO(), log, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
database, err = pebble.New(cfg.DatabasePath, cfg.DBCacheSize, dbLog)
database, err = pebble.New(cfg.DatabasePath, cfg.DBCacheSize, cfg.DBMaxHandles, dbLog)
}
if err != nil {
return nil, fmt.Errorf("open DB: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestNetworkVerificationOnNonEmptyDB(t *testing.T) {
t.Run(description, func(t *testing.T) {
dbPath := t.TempDir()
log := utils.NewNopZapLogger()
database, err := pebble.New(dbPath, 1, log)
database, err := pebble.New(dbPath, 1, 1, log)
require.NoError(t, err)
chain := blockchain.New(database, network, log)
syncer := sync.New(chain, adaptfeeder.New(feeder.NewTestClient(t, network)), log, 0, false)
Expand Down

0 comments on commit 09a2cf7

Please sign in to comment.