add cache configurations

pull/4618/head
“GheisMohammadi” 10 months ago
parent 8d24b5beef
commit d1ffd5cc75
No known key found for this signature in database
GPG Key ID: 15073AED3829FE90
  1. 29
      cmd/harmony/config.go
  2. 13
      cmd/harmony/config_migrations.go
  3. 39
      cmd/harmony/default.go
  4. 91
      cmd/harmony/flags.go
  5. 80
      cmd/harmony/flags_test.go
  6. 1
      cmd/harmony/main.go
  7. 13
      internal/configs/harmony/harmony.go
  8. 18
      internal/shardchain/shardchains.go

@ -145,6 +145,35 @@ func getDefaultSyncConfig(nt nodeconfig.NetworkType) harmonyconfig.SyncConfig {
} }
} }
func getDefaultCacheConfig(nt nodeconfig.NetworkType) harmonyconfig.CacheConfig {
cacheConfig := harmonyconfig.CacheConfig{
Disabled: defaultCacheConfig.Disabled,
TrieNodeLimit: defaultCacheConfig.TrieNodeLimit,
TriesInMemory: defaultCacheConfig.TriesInMemory,
TrieTimeLimit: defaultCacheConfig.TrieTimeLimit,
SnapshotLimit: defaultCacheConfig.SnapshotLimit,
SnapshotWait: defaultCacheConfig.SnapshotWait,
Preimages: defaultCacheConfig.Preimages,
SnapshotNoBuild: defaultCacheConfig.SnapshotNoBuild,
}
switch nt {
case nodeconfig.Mainnet:
cacheConfig.Disabled = true
cacheConfig.Preimages = true
case nodeconfig.Testnet:
cacheConfig.Disabled = false
cacheConfig.Preimages = true
case nodeconfig.Localnet:
cacheConfig.Disabled = false
cacheConfig.Preimages = false
default:
cacheConfig.Disabled = false
cacheConfig.Preimages = true
}
return cacheConfig
}
var configCmd = &cobra.Command{ var configCmd = &cobra.Command{
Use: "config", Use: "config",
Short: "dump or update config", Short: "dump or update config",

@ -334,7 +334,7 @@ func init() {
migrations["2.5.11"] = func(confTree *toml.Tree) *toml.Tree { migrations["2.5.11"] = func(confTree *toml.Tree) *toml.Tree {
if confTree.Get("General.TriesInMemory") == nil { if confTree.Get("General.TriesInMemory") == nil {
confTree.Set("General.TriesInMemory", defaultConfig.General.TriesInMemory) confTree.Set("General.TriesInMemory", defaultConfig.Cache.TriesInMemory)
} }
confTree.Set("Version", "2.5.12") confTree.Set("Version", "2.5.12")
return confTree return confTree
@ -405,6 +405,17 @@ func init() {
return confTree return confTree
} }
migrations["2.6.0"] = func(confTree *toml.Tree) *toml.Tree {
confTree.Delete("General.TriesInMemory")
if confTree.Get("Cache") == nil {
confTree.Set("Cache", defaultConfig.Cache)
}
// upgrade minor version because of `Cache` section introduction
confTree.Set("Version", "2.6.1")
return confTree
}
// check that the latest version here is the same as in default.go // check that the latest version here is the same as in default.go
largestKey := getNextVersion(migrations) largestKey := getNextVersion(migrations)
if largestKey != tomlConfigVersion { if largestKey != tomlConfigVersion {

@ -1,13 +1,15 @@
package main package main
import ( import (
"time"
"github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/hmy" "github.com/harmony-one/harmony/hmy"
harmonyconfig "github.com/harmony-one/harmony/internal/configs/harmony" harmonyconfig "github.com/harmony-one/harmony/internal/configs/harmony"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node" nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
) )
const tomlConfigVersion = "2.6.0" const tomlConfigVersion = "2.6.1"
const ( const (
defNetworkType = nodeconfig.Mainnet defNetworkType = nodeconfig.Mainnet
@ -24,7 +26,6 @@ var defaultConfig = harmonyconfig.HarmonyConfig{
IsOffline: false, IsOffline: false,
DataDir: "./", DataDir: "./",
TraceEnable: false, TraceEnable: false,
TriesInMemory: 128,
}, },
Network: getDefaultNetworkConfig(defNetworkType), Network: getDefaultNetworkConfig(defNetworkType),
P2P: harmonyconfig.P2pConfig{ P2P: harmonyconfig.P2pConfig{
@ -131,6 +132,7 @@ var defaultConfig = harmonyconfig.HarmonyConfig{
LowUsageThreshold: hmy.DefaultGPOConfig.LowUsageThreshold, LowUsageThreshold: hmy.DefaultGPOConfig.LowUsageThreshold,
BlockGasLimit: hmy.DefaultGPOConfig.BlockGasLimit, BlockGasLimit: hmy.DefaultGPOConfig.BlockGasLimit,
}, },
Cache: getDefaultCacheConfig(defNetworkType),
} }
var defaultSysConfig = harmonyconfig.SysConfig{ var defaultSysConfig = harmonyconfig.SysConfig{
@ -176,7 +178,7 @@ var defaultPrometheusConfig = harmonyconfig.PrometheusConfig{
} }
var defaultStagedSyncConfig = harmonyconfig.StagedSyncConfig{ var defaultStagedSyncConfig = harmonyconfig.StagedSyncConfig{
TurboMode: true, TurboMode: false,
DoubleCheckBlockHashes: false, DoubleCheckBlockHashes: false,
MaxBlocksPerSyncCycle: 512, // sync new blocks in each cycle, if set to zero means all blocks in one full cycle MaxBlocksPerSyncCycle: 512, // sync new blocks in each cycle, if set to zero means all blocks in one full cycle
MaxBackgroundBlocks: 512, // max blocks to be downloaded at background process in turbo mode MaxBackgroundBlocks: 512, // max blocks to be downloaded at background process in turbo mode
@ -228,14 +230,14 @@ var (
Downloader: true, Downloader: true,
StagedSync: true, StagedSync: true,
StagedSyncCfg: defaultStagedSyncConfig, StagedSyncCfg: defaultStagedSyncConfig,
Concurrency: 4, Concurrency: 2,
MinPeers: 4, MinPeers: 2,
InitStreams: 4, InitStreams: 2,
MaxAdvertiseWaitTime: 5, //minutes MaxAdvertiseWaitTime: 1, //minutes
DiscSoftLowCap: 4, DiscSoftLowCap: 2,
DiscHardLowCap: 4, DiscHardLowCap: 2,
DiscHighCap: 1024, DiscHighCap: 1024,
DiscBatch: 8, DiscBatch: 3,
} }
defaultPartnerSyncConfig = harmonyconfig.SyncConfig{ defaultPartnerSyncConfig = harmonyconfig.SyncConfig{
@ -271,6 +273,17 @@ var (
} }
) )
var defaultCacheConfig = harmonyconfig.CacheConfig{
Disabled: false,
TrieNodeLimit: 256,
TriesInMemory: 128,
TrieTimeLimit: 2 * time.Minute,
SnapshotLimit: 256,
SnapshotWait: true,
Preimages: true,
SnapshotNoBuild: false,
}
const ( const (
defaultBroadcastInvalidTx = false defaultBroadcastInvalidTx = false
) )
@ -285,6 +298,7 @@ func getDefaultHmyConfigCopy(nt nodeconfig.NetworkType) harmonyconfig.HarmonyCon
} }
config.Sync = getDefaultSyncConfig(nt) config.Sync = getDefaultSyncConfig(nt)
config.DNSSync = getDefaultDNSSyncConfig(nt) config.DNSSync = getDefaultDNSSyncConfig(nt)
config.Cache = getDefaultCacheConfig(nt)
return config return config
} }
@ -324,6 +338,11 @@ func getDefaultPrometheusConfigCopy() harmonyconfig.PrometheusConfig {
return config return config
} }
func getDefaultCacheConfigCopy() harmonyconfig.CacheConfig {
config := defaultCacheConfig
return config
}
const ( const (
nodeTypeValidator = "validator" nodeTypeValidator = "validator"
nodeTypeExplorer = "explorer" nodeTypeExplorer = "explorer"

@ -32,7 +32,6 @@ var (
legacyDataDirFlag, legacyDataDirFlag,
taraceFlag, taraceFlag,
triesInMemoryFlag,
} }
dnsSyncFlags = []cli.Flag{ dnsSyncFlags = []cli.Flag{
@ -268,6 +267,16 @@ var (
gpoBlockGasLimitFlag, gpoBlockGasLimitFlag,
} }
cacheConfigFlags = []cli.Flag{
cacheDisabled,
cacheTrieNodeLimit,
cacheTriesInMemory,
cachePreimages,
cacheSnapshotLimit,
cacheSnapshotNoBuild,
cacheSnapshotWait,
}
metricsFlags = []cli.Flag{ metricsFlags = []cli.Flag{
metricsETHFlag, metricsETHFlag,
metricsExpensiveETHFlag, metricsExpensiveETHFlag,
@ -352,11 +361,6 @@ var (
Usage: "indicates if full transaction tracing should be enabled", Usage: "indicates if full transaction tracing should be enabled",
DefValue: defaultConfig.General.TraceEnable, DefValue: defaultConfig.General.TraceEnable,
} }
triesInMemoryFlag = cli.IntFlag{
Name: "blockchain.tries_in_memory",
Usage: "number of blocks from header stored in disk before exiting",
DefValue: defaultConfig.General.TriesInMemory,
}
) )
func getRootFlags() []cli.Flag { func getRootFlags() []cli.Flag {
@ -436,14 +440,6 @@ func applyGeneralFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig)
if cli.IsFlagChanged(cmd, isBackupFlag) { if cli.IsFlagChanged(cmd, isBackupFlag) {
config.General.IsBackup = cli.GetBoolFlagValue(cmd, isBackupFlag) config.General.IsBackup = cli.GetBoolFlagValue(cmd, isBackupFlag)
} }
if cli.IsFlagChanged(cmd, triesInMemoryFlag) {
value := cli.GetIntFlagValue(cmd, triesInMemoryFlag)
if value <= 2 {
panic("Must provide number greater than 2 for General.TriesInMemory")
}
config.General.TriesInMemory = value
}
} }
// network flags // network flags
@ -2115,3 +2111,70 @@ func applyGPOFlags(cmd *cobra.Command, cfg *harmonyconfig.HarmonyConfig) {
cfg.GPO.BlockGasLimit = cli.GetIntFlagValue(cmd, gpoBlockGasLimitFlag) cfg.GPO.BlockGasLimit = cli.GetIntFlagValue(cmd, gpoBlockGasLimitFlag)
} }
} }
// cache config flags
var (
cacheDisabled = cli.BoolFlag{
Name: "cache.disabled",
Usage: "Whether to disable trie write caching (archive node)",
DefValue: defaultCacheConfig.Disabled,
}
cacheTrieNodeLimit = cli.IntFlag{
Name: "cache.trie_node_limit",
Usage: " Memory limit (MB) at which to flush the current in-memory trie to disk",
DefValue: defaultCacheConfig.TrieNodeLimit,
}
cacheTriesInMemory = cli.Uint64Flag{
Name: "cache.tries_in_memory",
Usage: "Block number from the head stored in disk before exiting",
DefValue: defaultCacheConfig.TriesInMemory,
}
cachePreimages = cli.BoolFlag{
Name: "cache.preimages",
Usage: "Whether to store preimage of trie key to the disk",
DefValue: defaultCacheConfig.Preimages,
}
cacheSnapshotLimit = cli.IntFlag{
Name: "cache.snapshot_limit",
Usage: "Memory allowance (MB) to use for caching snapshot entries in memory",
DefValue: defaultCacheConfig.SnapshotLimit,
}
cacheSnapshotNoBuild = cli.BoolFlag{
Name: "cache.snapshot_no_build",
Usage: "Whether the background generation is allowed",
DefValue: defaultCacheConfig.SnapshotNoBuild,
}
cacheSnapshotWait = cli.BoolFlag{
Name: "cache.snapshot_wait",
Usage: "Wait for snapshot construction on startup",
DefValue: defaultCacheConfig.SnapshotWait,
}
)
func applyCacheFlags(cmd *cobra.Command, cfg *harmonyconfig.HarmonyConfig) {
if cli.IsFlagChanged(cmd, cacheDisabled) {
cfg.Cache.Disabled = cli.GetBoolFlagValue(cmd, cacheDisabled)
}
if cli.IsFlagChanged(cmd, cacheTrieNodeLimit) {
cfg.Cache.TrieNodeLimit = cli.GetIntFlagValue(cmd, cacheTrieNodeLimit)
}
if cli.IsFlagChanged(cmd, cacheTriesInMemory) {
value := cli.GetUint64FlagValue(cmd, cacheTriesInMemory)
if value <= 2 {
panic("Must provide number greater than 2 for Cache.TriesInMemory")
}
cfg.Cache.TriesInMemory = value
}
if cli.IsFlagChanged(cmd, cachePreimages) {
cfg.Cache.Preimages = cli.GetBoolFlagValue(cmd, cachePreimages)
}
if cli.IsFlagChanged(cmd, cacheSnapshotLimit) {
cfg.Cache.SnapshotLimit = cli.GetIntFlagValue(cmd, cacheSnapshotLimit)
}
if cli.IsFlagChanged(cmd, cacheSnapshotNoBuild) {
cfg.Cache.SnapshotNoBuild = cli.GetBoolFlagValue(cmd, cacheSnapshotNoBuild)
}
if cli.IsFlagChanged(cmd, cacheSnapshotWait) {
cfg.Cache.SnapshotWait = cli.GetBoolFlagValue(cmd, cacheSnapshotWait)
}
}

@ -42,7 +42,6 @@ func TestHarmonyFlags(t *testing.T) {
ShardID: -1, ShardID: -1,
IsArchival: false, IsArchival: false,
DataDir: "./", DataDir: "./",
TriesInMemory: 128,
}, },
Network: harmonyconfig.NetworkConfig{ Network: harmonyconfig.NetworkConfig{
NetworkType: "mainnet", NetworkType: "mainnet",
@ -183,6 +182,16 @@ func TestHarmonyFlags(t *testing.T) {
LowUsageThreshold: defaultConfig.GPO.LowUsageThreshold, LowUsageThreshold: defaultConfig.GPO.LowUsageThreshold,
BlockGasLimit: defaultConfig.GPO.BlockGasLimit, BlockGasLimit: defaultConfig.GPO.BlockGasLimit,
}, },
Cache: harmonyconfig.CacheConfig{
Disabled: defaultConfig.Cache.Disabled,
TrieNodeLimit: defaultCacheConfig.TrieNodeLimit,
TriesInMemory: defaultConfig.Cache.TriesInMemory,
TrieTimeLimit: defaultConfig.Cache.TrieTimeLimit,
SnapshotLimit: defaultConfig.Cache.SnapshotLimit,
SnapshotWait: defaultConfig.Cache.SnapshotWait,
Preimages: defaultConfig.Cache.Preimages,
SnapshotNoBuild: defaultConfig.Cache.SnapshotNoBuild,
},
}, },
}, },
} }
@ -213,7 +222,6 @@ func TestGeneralFlags(t *testing.T) {
ShardID: -1, ShardID: -1,
IsArchival: false, IsArchival: false,
DataDir: "./", DataDir: "./",
TriesInMemory: 128,
}, },
}, },
{ {
@ -225,7 +233,6 @@ func TestGeneralFlags(t *testing.T) {
ShardID: 0, ShardID: 0,
IsArchival: true, IsArchival: true,
DataDir: "./.hmy", DataDir: "./.hmy",
TriesInMemory: 128,
}, },
}, },
{ {
@ -237,7 +244,6 @@ func TestGeneralFlags(t *testing.T) {
ShardID: 0, ShardID: 0,
IsArchival: true, IsArchival: true,
DataDir: "./", DataDir: "./",
TriesInMemory: 128,
}, },
}, },
{ {
@ -248,7 +254,6 @@ func TestGeneralFlags(t *testing.T) {
ShardID: -1, ShardID: -1,
IsArchival: false, IsArchival: false,
DataDir: "./", DataDir: "./",
TriesInMemory: 128,
}, },
}, },
{ {
@ -259,7 +264,6 @@ func TestGeneralFlags(t *testing.T) {
ShardID: 0, ShardID: 0,
IsArchival: false, IsArchival: false,
DataDir: "./", DataDir: "./",
TriesInMemory: 128,
}, },
}, },
{ {
@ -270,18 +274,6 @@ func TestGeneralFlags(t *testing.T) {
ShardID: 0, ShardID: 0,
IsArchival: false, IsArchival: false,
DataDir: "./", DataDir: "./",
TriesInMemory: 128,
},
},
{
args: []string{"--blockchain.tries_in_memory", "64"},
expConfig: harmonyconfig.GeneralConfig{
NodeType: "validator",
NoStaking: false,
ShardID: -1,
IsArchival: false,
DataDir: "./",
TriesInMemory: 64,
}, },
}, },
} }
@ -1435,6 +1427,58 @@ func TestGPOFlags(t *testing.T) {
} }
} }
func TestCacheFlags(t *testing.T) {
tests := []struct {
args []string
expConfig harmonyconfig.CacheConfig
expErr error
}{
{
args: []string{},
expConfig: harmonyconfig.CacheConfig{
Disabled: true, // based on network type
TrieNodeLimit: defaultCacheConfig.TrieNodeLimit,
TriesInMemory: defaultCacheConfig.TriesInMemory,
TrieTimeLimit: defaultCacheConfig.TrieTimeLimit,
SnapshotLimit: defaultCacheConfig.SnapshotLimit,
SnapshotWait: defaultCacheConfig.SnapshotWait,
Preimages: defaultCacheConfig.Preimages, // based on network type
SnapshotNoBuild: defaultCacheConfig.SnapshotNoBuild,
},
},
{
args: []string{"--cache.disabled=true", "--cache.trie_node_limit", "512", "--cache.tries_in_memory", "256", "--cache.preimages=false", "--cache.snapshot_limit", "512", "--cache.snapshot_no_build=true", "--cache.snapshot_wait=false"},
expConfig: harmonyconfig.CacheConfig{
Disabled: true,
TrieNodeLimit: 512,
TriesInMemory: 256,
TrieTimeLimit: 2 * time.Minute,
SnapshotLimit: 512,
SnapshotWait: false,
Preimages: false,
SnapshotNoBuild: true,
},
},
}
for i, test := range tests {
ts := newFlagTestSuite(t, cacheConfigFlags, applyCacheFlags)
hc, err := ts.run(test.args)
if assErr := assertError(err, test.expErr); assErr != nil {
t.Fatalf("Test %v: %v", i, assErr)
}
if err != nil || test.expErr != nil {
continue
}
if !reflect.DeepEqual(hc.Cache, test.expConfig) {
t.Errorf("Test %v:\n\t%+v\n\t%+v", i, hc.Cache, test.expConfig)
}
ts.tearDown()
}
}
func TestDevnetFlags(t *testing.T) { func TestDevnetFlags(t *testing.T) {
tests := []struct { tests := []struct {
args []string args []string

@ -245,6 +245,7 @@ func applyRootFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) {
applySyncFlags(cmd, config) applySyncFlags(cmd, config)
applyShardDataFlags(cmd, config) applyShardDataFlags(cmd, config)
applyGPOFlags(cmd, config) applyGPOFlags(cmd, config)
applyCacheFlags(cmd, config)
} }
func setupNodeLog(config harmonyconfig.HarmonyConfig) { func setupNodeLog(config harmonyconfig.HarmonyConfig) {

@ -37,6 +37,7 @@ type HarmonyConfig struct {
ShardData ShardDataConfig ShardData ShardDataConfig
GPO GasPriceOracleConfig GPO GasPriceOracleConfig
Preimage *PreimageConfig Preimage *PreimageConfig
Cache CacheConfig
} }
func (hc HarmonyConfig) ToRPCServerConfig() nodeconfig.RPCServerConfig { func (hc HarmonyConfig) ToRPCServerConfig() nodeconfig.RPCServerConfig {
@ -138,7 +139,6 @@ type GeneralConfig struct {
TraceEnable bool TraceEnable bool
EnablePruneBeaconChain bool EnablePruneBeaconChain bool
RunElasticMode bool RunElasticMode bool
TriesInMemory int
} }
type TiKVConfig struct { type TiKVConfig struct {
@ -306,6 +306,17 @@ type RevertConfig struct {
RevertBefore int RevertBefore int
} }
type CacheConfig struct {
Disabled bool // Whether to disable trie write caching (archive node)
TrieNodeLimit int // Memory limit (MB) at which to flush the current in-memory trie to disk
TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk
TriesInMemory uint64 // Block number from the head stored in disk before exiting
Preimages bool // Whether to store preimage of trie key to the disk
SnapshotLimit int // Memory allowance (MB) to use for caching snapshot entries in memory
SnapshotNoBuild bool // Whether the background generation is allowed
SnapshotWait bool // Wait for snapshot construction on startup
}
type PreimageConfig struct { type PreimageConfig struct {
ImportFrom string ImportFrom string
ExportTo string ExportTo string

@ -3,7 +3,6 @@ package shardchain
import ( import (
"math/big" "math/big"
"sync" "sync"
"time"
"github.com/harmony-one/harmony/core/state" "github.com/harmony-one/harmony/core/state"
harmonyconfig "github.com/harmony-one/harmony/internal/configs/harmony" harmonyconfig "github.com/harmony-one/harmony/internal/configs/harmony"
@ -110,14 +109,19 @@ func (sc *CollectionImpl) ShardChain(shardID uint32, options ...core.Options) (c
Uint32("shardID", shardID). Uint32("shardID", shardID).
Msg("disable cache, running in archival mode") Msg("disable cache, running in archival mode")
} else { } else {
hc := sc.harmonyconfig
if hc != nil {
cacheConfig = &core.CacheConfig{ cacheConfig = &core.CacheConfig{
TrieNodeLimit: 256, Disabled: hc.Cache.Disabled,
TrieTimeLimit: 2 * time.Minute, TrieNodeLimit: hc.Cache.TrieNodeLimit,
TriesInMemory: 128, TrieTimeLimit: hc.Cache.TrieTimeLimit,
Preimages: true, TriesInMemory: hc.Cache.TriesInMemory,
SnapshotLimit: hc.Cache.SnapshotLimit,
SnapshotWait: hc.Cache.SnapshotWait,
Preimages: hc.Cache.Preimages,
} }
if sc.harmonyconfig != nil { } else {
cacheConfig.TriesInMemory = uint64(sc.harmonyconfig.General.TriesInMemory) cacheConfig = nil
} }
} }

Loading…
Cancel
Save